<?php

include_once ('bn_path.php');

class bn_captcha
{
    public $font_size = 5;
    public $fontfilename = '';
    public $bg_color = array(255,255,255);
    public $text_color = array(0,0,0);
    public $line = 0;
    public $noise = 0;
    private $img;
    private $gf;
    private $session;
    protected static $instance;


    function __construct()
    {
        $this->img = new bn_images;
        $this->gf=bn_genralfunc::getInstance();
        $this->session=new bn_session('__captcha');
    }

    public static function getInstance()
    {
        if (!isset(self::$instance))
        {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    
    public function add_captchaphp()
    {
        $text = '<?php
        include_once("../../libs/bn_path.php");
        $cap=new bn_captcha();
        $cap->noise=200;
        $cap->line=3;
        $cap->Create_captcha_line();
        ?>';
        if (!is_dir('./captcha'))
        {
            mkdir('./captcha/', 777);
        }
        if (!file_exists('./captcha/captcha.php'))
        {
            $fd=bn_files::getInstance();
            $fd->Write_file('./captcha/captcha.php', $text);
        }
            
    }
    
    public function check_captcha($post)
    {
     if($post==$this->session->captcha)
     return true;
     else
     return false;  
    }

    public function Create_captcha_line()
    {
        header("Content-type: image/png");
        $text=$this->gf->RandomStrgenerate(5);
        $this->session->captcha=strtolower($text);
        $width = (imagefontwidth($this->font_size) * strlen($text)) + 30;
        $height = imagefontheight($this->font_size) + 30;
        $handle = ImageCreate($width, $height) or die("Cannot Create image");
        $bcolor = imagecolorallocate($handle, $this->bg_color[0], $this->bg_color[1], $this->bg_color[2]);


        if ($this->line > 0)
        {
            $minwidth = intval($width / 4);
            for ($i = 0; $i <= $this->line; $i++)
            {
                imageline($handle, rand(1, $minwidth), rand(1, $height), rand($width - $minwidth,
                    $width), rand(1, $height), ImageColorAllocate($handle, rand(1, 255), rand(1, 255),
                    rand(1, 255)));
            }
        }


        $txt_color = imagecolorallocate($handle, $this->text_color[0], $this->
            text_color[1], $this->text_color[2]);
        if ($this->fontfilename == '')
            ImageString($handle, 5, 15, 15, $text, $txt_color);
        else
            imagettftext($handle, $this->font_size, 0, 15, 15, $txt_color, $this->
                fontfilename, $text);

        if ($this->noise > 0)
        {
            for ($i = 0; $i <= $this->noise; $i++)
            {
                imagesetpixel($handle, rand(1, $width), rand(1, $height), ImageColorAllocate($handle,rand(1, 255), rand(1, 255), rand(1, 255)));
            }
        }
        
        ImagePng($handle);
        imagedestroy($handle);
    }


    public function Create_captcha_rectangle($text, $filledellipse = false)
    {
        $width = (imagefontwidth($this->font_size) * strlen($text)) + 30;
        $height = imagefontheight($this->font_size) + 30;
        $handle = ImageCreate($width, $height) or die("Cannot Create image");
        $bg_color = $this->img->rgb2int($bg_color);
        $txt_color = $this->img->rgb2int($txt_color);

        $medgray = $this->img->rgb2int(array(180,180,180));
        $lightgray = imagecolorallocate(array(220,220,220));

        if ($this->noise > 0)
        {
            for ($i = 0; $i <= $this->noise; $i++)
            {
                imagesetpixel($handle, rand(1, $width), rand(1, $height), ImageColorAllocate($handle,rand(1, 255), rand(1, 255), rand(1, 255)));
            }
        }


        for ($i = 0; $i < 10; $i++)
        {
            $cx = (int)rand(0, $width - 5);
            $cy = (int)rand(1, $height - 5);
            $h = rand(1, 10);
            $w = rand(1, 10);
            if ($filledellipse)
                imageellipse($handle, $cx, $cy, $w, $h, $lightgray);
            else
                imagefilledellipse($handle, $cx, $cy, $w, $h, $lightgray);
        }

        header("Content-type: image/png");
        if ($this->fontfilename == '')
            ImageString($handle, 5, 15, 15, $text, $txt_color);
        else
            imagettftext($handle, $this->font_size, 0, 15, 15, $txt_color, $this->
                fontfilename, $text);
        ImagePng($handle);
    }
    
    function __destruct()
    {
       unset($this->gf);
       unset($this->img);
    }
}

?>